const util = require('../../utils/util.js')
const App = getApp()
const plugin = requirePlugin('bb-plugin') // 引入插件
Page({
  data: {
    navStyle: [],
    islogin: false,
    userInfo: {},
    nickName: '',
    mobile: null,
    hyType: '白银会员',
    headimg: '',
    guangGaoUp: [],
    guangGaoDown: [],
    apiUrl: util.apiUrl,
    ucenterList: {},
    autoplay: true,
    interval: 5000,
    duration: 500,
    obligationNum: 0,
    hyList: [],
    cardInfo: {
      czye: '3.97',
      integral: 458
    },
    topNavImage: util.Background_base64('static/image/my_images/headerbj.png'),
    swiperList: [
      {
        value: 'https://img.xnvzj.com/28/1093cf893ab22ce50737caf733d205.jpg',
        ariaLabel: '图片1'
      }
    ],
    current: 0,
    popVisible: false,
    bbPlugin: false
  },
  //获取列表数据--类别
  async getData(e) {
    var that = this
    try {
      let token = wx.getStorageSync('token') || ''
      if (!token) return

      await util.request('/api/ucenter/index', {}, 'GET').then((res) => {
        var datas = res.data.data
        that.setData({
          ucenterList: datas
        })
      })
    } catch (err) {
      wx.showToast({
        title: err
      })
    }
  },
  /**
   *
   * b2b门店助手
   */
  handlePopup(e) {
    if (this.data.bbPlugin) {
      this.goBindB2b()
      return
    }

    this.setData({ popVisible: true })
  },
  onVisibleChange(e) {
    console.log(e)
    this.setData({
      popVisible: e.detail.visible
    })
  },
  onClose() {
    this.setData({
      popVisible: false
    })
  },
  goBindB2b() {
    wx.navigateTo({
      url: 'plugin://bb-plugin/info-regist'
    })
  },
  onLoad() {
    let that = this
    that.setData({
      navStyle: App.globalData,
      myImageUrl: util.Background_base64('static/image/my_images/headerbj.png')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    plugin.getRetailInfo({
      success: (res) => {
        if (res.status) {
          this.data.bbPlugin = true
        }
      }
    })

    let that = this
    let nameInfoObject = wx.getStorageSync('nameInfoObject')
    if (nameInfoObject) {
      that.setData({
        nickName: nameInfoObject.nickName,
        headimg: nameInfoObject.avatarUrl,
        mobile: nameInfoObject.mobile,
        islogin: true
      })
    } else {
      this.setData({
        islogin: false
      })
    }
    that.getData()
  },

  // 登录
  bindgetuserinfo: function () {
    wx.navigateTo({
      url: '/pages/gologin/gologin'
    })
  },

  toOrder: function (e) {
    if (util.isLogin() === true) {
      wx.navigateTo({
        url: '/modules/userModule/pages/userorders/userorders?status=' + e.currentTarget.dataset.state
      })
    }
  },
  // 上传图片
  bindUpload: function (e) {
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 3
        break
      case 1:
        this.data.count = 2
        break
      case 2:
        this.data.count = 1
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: 'https://example.weixin.qq.com/upload',
            filePath: tempFilePaths[i],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: function (res) {
              if (res.statusCode == 200) {
                wx.showToast({
                  title: '上传成功',
                  icon: 'none',
                  duration: 1500
                })

                that.data.imgs.push(JSON.parse(res.data).data)

                that.setData({
                  imgs: that.data.imgs
                })
              }
            },
            fail: function (err) {
              wx.showToast({
                title: '上传失败',
                icon: 'none',
                duration: 2000
              })
            },
            complete: function (result) {
              console.log(result.errMsg)
            }
          })
        }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否删除',
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.imgs.length; i++) {
            if (i == e.currentTarget.dataset.index) that.data.imgs.splice(i, 1)
          }
          that.setData({
            imgs: that.data.imgs
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  onShareAppMessage: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
      menu: ['shareAppMessage', 'shareTimeline']
    })
    return {
      title: '我要发出去的显示的标题1111',
      imageUrl: '/static/icon/shouye.png',
      path: '/pages/share_page/share_page?uid=11111'
      // query:'uid=11111'
    }
  },
  onShareTimeline: function () {
    return {
      title: '分享给朋友的标题2222',
      imageUrl: '/static/icon/shouye.png',
      query: 'uid=11112'
    }
  },
  onAddToFavorites: function (options) {
    return {
      title: '微信收藏上的标题3333',
      imageUrl: '/static/icon/shouye.png',
      query: 'uid=11113'
    }
  },
  showDialog(e) {
    const { key } = e.currentTarget.dataset
    this.setData({
      [key]: true,
      dialogKey: key
    })
  },
  closeDialog() {
    const { dialogKey } = this.data
    this.setData({
      [dialogKey]: false
    })
  }
})
